﻿#region Copyright © 2013 Miracle Business Solutions
// All rights are reserved. Reproduction or transmission in whole or in part,
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written consent of the copyright owner.
// 
// Document:	RRLoader\GUI.cs
// Creation Date:	11/11/2013
// Last Edit:		11/11/2013
// Author:		Moreno Sint Hill
#endregion

using System;
using System.Windows.Forms;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;

namespace RRLoader
{
    public partial class GUI : Form
    {
        public GUI()
        {
            InitializeComponent();
            CRList.DataSource = RoutineManager.Routines;
            RRLoader.Backup = RoutineManager.Current;
        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
            var selected = (ICombatRoutine) CRList.SelectedItem;
            if (selected != null)
            {
                try
                {
                    TreeRoot.Stop("[RRLoader] -- Stopping threads, to load new routine");
                    //Call Dispose, Shutdown.. to make sure it's gone
                    RoutineManager.Current.Dispose(); // Calls RoutineManager.Current.ShutDown();
                    //Replace current routine with a invalidroutine wrapper
                    RoutineManager.Current = new InvalidRoutineWrapper(); //Prevent singular from re-hooking on next move
                    //Clear all hooks
                    TreeHooks.Instance.ClearAll();
                    //Hook in the selected one
                    RoutineManager.Current = selected;
                    //BUG: RoutineManager.Current.Initialize(); Automatically
                    TreeRoot.Start();
                    MessageBox.Show("Combat Routine loaded", "RRLoader Notification");
                    Logging.Write("[RRLoader] -- New routine loaded succesfully");
                }
                catch
                {
                    if (RoutineManager.Current != null && RoutineManager.Current != RRLoader.Backup)
                        RoutineManager.Current = RRLoader.Backup;
                }
            }
        }

    }
}
